package com.glut.controller;

import com.github.pagehelper.Page;
import com.glut.service.SstsFeedbackService;
import com.glut.vo.SstsFeedback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:沈林玉
 * @date:2020/3/13 17:31
 * @Description:
 */
@Controller
@RequestMapping("/feedback")
public class SstsFeedbackController {

    @Autowired
    SstsFeedbackService sstsFeedbackService;

    /**
     * 1.查看未处理的反馈
     * @param sstsFeedback
     * @param session
     * @return
     */
    @RequestMapping("/selectAllNot/{sstsUserID}/{userType}/{pageNum}")
    public String selectAllNotFeedback(@PathVariable String sstsUserID,
                                        @PathVariable String userType,
                                        SstsFeedback sstsFeedback,
                                        Integer pageNum,
                                        HttpSession session){
        if(pageNum==null||pageNum< 1){
            // 保留查询条件
            pageNum=1;
        }
        // 判断用户是学生还是教师(1:教师，0：学生)
        if (userType.equals("0")){
            // 判断是哪个学生用户并进行过滤条件
            Page<SstsFeedback> selectNotFeedbackByUserIdList = ( Page<SstsFeedback>)sstsFeedbackService.selectNotFeedbackByUserId(sstsUserID,pageNum,10);
            session.setAttribute("sstsFeedbackNotByIdList",selectNotFeedbackByUserIdList);
            return "redirect:/views/feedback/feedback_selectNotById.jsp";
        }

        Page<SstsFeedback> sstsFeedbackList = (Page<SstsFeedback>)sstsFeedbackService.selectAllNotFeedback(sstsFeedback,pageNum,10);

        session.setAttribute("sstsFeedbackNotList",sstsFeedbackList);
        return "redirect:/views/feedback/feedback_selectNot.jsp";
    }

    /**
     * 2.查看已处理的反馈
     * @param sstsFeedback
     * @param session
     * @return
     */
    @RequestMapping("/selectAllDeal/{sstsUserId}/{userType}/{pageNum}")
    public String selectAllDealFeedback(@PathVariable String sstsUserId,
                                         @PathVariable String userType,
                                         SstsFeedback sstsFeedback,
                                         Integer pageNum,
                                         HttpSession session){
        if(pageNum==null||pageNum< 1){
            // 保留查询条件
            pageNum=1;
        }
        // 判断用户是学生还是教师(1:教师，0：学生)
        if ("0".equals(userType)){
            // 判断是哪个学生用户并进行过滤条件
            Page<SstsFeedback> selectDealFeedbackByUserIdList = (Page<SstsFeedback>)sstsFeedbackService.selectDealFeedbackByUserId(sstsUserId,pageNum,10);
            session.setAttribute("sstsFeedbackDealByIdList",selectDealFeedbackByUserIdList);
            System.out.println(selectDealFeedbackByUserIdList.getTotal());
            return "redirect:/views/feedback/feedback_selectDealById.jsp";
        }

        Page<SstsFeedback> sstsFeedbackList = (Page<SstsFeedback>)sstsFeedbackService.selectAllDealFeedback(sstsFeedback,pageNum,10);

        session.setAttribute("sstsFeedbackDealList",sstsFeedbackList);
        return "redirect:/views/feedback/feedback_selectDeal.jsp";
    }

    /**
     * 3.处理反馈
     * @param sstsFeedback
     * @return
     */
    @RequestMapping("/updateNotFeedback/{feedbackType}")
    @ResponseBody
    public Map<String,Integer> updateNotFeedback(@PathVariable Integer feedbackType,
                                                 SstsFeedback sstsFeedback,
                                                 HttpSession session){
        // 已处理反馈更新为未处理
        if (feedbackType==0){
            sstsFeedback.setFeedbackStatus(0);
        }else {
            // 未处理反馈更新为已处理
            sstsFeedback.setFeedbackStatus(1);
        }
        int updateNotFeedback = sstsFeedbackService.updateNotFeedback(sstsFeedback);
        Map<String,Integer> map = new HashMap<String, Integer>();
        map.put("updateNotFeedback",updateNotFeedback);
        return map;
    }

    /**
     * 4.学生提交反馈信息
     * @param sstsFeedback
     * @return
     */
    @RequestMapping("/insertFeedback/{userId}")
    public ModelAndView insertFeedback(@PathVariable String userId,
                                  SstsFeedback sstsFeedback,
                                  HttpSession session){

        sstsFeedback.setSstsUserID(userId);
        sstsFeedback.setFeedbackStatus(0);// 设置反馈状态为0未处理
        // 进行反馈信息的数据库插入操作
        int insertNotFeedback = sstsFeedbackService.insertNotFeedback(sstsFeedback);

        // 存于session中，便于页面取出进行相应判断成功与否
        session.setAttribute("insertNotFeedback",insertNotFeedback);
        ModelAndView modelAndView=new ModelAndView("redirect:/feedback/selectAllNot/\"+userId+\"/\"+0+\"/\"+1");

        return modelAndView;

    }

}
